package com.ljye.jg.ljye.activity;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ljye.jg.ljye.R;

public class Activity_Retrieve extends Activity {
    public View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.back://返回
                    finish();
                    break;
                case R.id.sms_pass://获取验证码

                    break;
                case R.id.login://提交

                    break;
                default:
                    break;
            }
        }
    };
    private ImageView back;//返回
    private EditText et_pwd;//输入密码
    private EditText phone;//输入手机号
    private Button smsPass;//验证码
    private EditText sms;//输入验证码
    private Button login;//提交
    private boolean isHidePwd = true;// 输入框密码是否是隐藏的，默认为true

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve);
        initView();
        initEdit();
        back.setOnClickListener(listener);
        smsPass.setOnClickListener(listener);
        login.setOnClickListener(listener);


    }


    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        et_pwd = (EditText) findViewById(R.id.password);
        phone = (EditText) findViewById(R.id.phone);
        smsPass = (Button) findViewById(R.id.sms_pass);
        sms = (EditText) findViewById(R.id.sms);
        login = (Button) findViewById(R.id.login);

    }

    private void initEdit() {
        final Drawable[] drawables = et_pwd.getCompoundDrawables();
        final int eyeWidth = drawables[2].getBounds().width();// 眼睛图标的宽度
        final Drawable drawableEyeOpen = getResources().getDrawable(R.drawable.open_eyes);
        drawableEyeOpen.setBounds(drawables[2].getBounds());//这一步不能省略
        et_pwd.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    // getWidth,getHeight必须在这里处理
                    float et_pwdMinX = v.getWidth() - eyeWidth - et_pwd.getPaddingRight();
                    float et_pwdMaxX = v.getWidth();
                    float et_pwdMinY = 0;
                    float et_pwdMaxY = v.getHeight();
                    float x = event.getX();
                    float y = event.getY();
                    if (x < et_pwdMaxX && x > et_pwdMinX && y > et_pwdMinY && y < et_pwdMaxY) {
                        // 点击了眼睛图标的位置
                        isHidePwd = !isHidePwd;
                        if (isHidePwd) {
                            et_pwd.setCompoundDrawables(drawables[0],
                                    drawables[1],
                                    drawables[2],
                                    drawables[3]);

                            et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                        } else {
                            et_pwd.setCompoundDrawables(drawables[0],
                                    drawables[1],
                                    drawableEyeOpen,
                                    drawables[3]);
                            et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        }
                    }
                }
                return false;
            }
        });
    }
}


